package com.zxy.javaarithmetic.chapter_2_LinkList;

/*
 *  @项目名：  study
 *  @包名：    com.zxy.javaarithmetic.chapter_2_LinkList
 *  @文件名:   P_4_ReverseLinkList
 *  @创建者:   zhangxy
 *  @创建时间:  2018/12/7 16:59
 *  @描述：    TODO
 */
public class P_4_ReverseLinkList {

	public static void main(String[] args) {

		Node head = LinkListUtils.getNode(5);
		LinkListUtils.printNode(head);
		LinkListUtils.printNode(reverseLinkedList(head));
	}

	/**
	 * 反转单向链表
	 * @param head
	 * @return
	 */
	private static Node reverseLinkedList(Node head) {
		Node pre = null;
		Node next;
		while (head != null) {
			next = head.next;
			head.next = pre;
			pre = head;
			head = next;
		}
		return pre;
		//		if (head != null) {
		//			Node next = head.next;
		//			head.next = null;
		//			while (next != null) {
		//				Node temp = next.next;
		//				next.next = head;
		//				head = next;
		//				next = temp;
		//			}
		//		}
		//		return head;
	}

	/**
	 * 反转双向链表
	 * @param head
	 * @return
	 */
	private static DoubleNode reverseDoubleNode(DoubleNode head) {
		DoubleNode pre = null;
		DoubleNode next = null;
		while (head != null) {
			next = head.next;
			head.next = pre;
			head.last = next;
			pre = head;
			head = next;
		}

		return head;
	}
}
